class Solution {
public:

    int minFallingPathSum(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0) dp[i][j] = matrix[i][j];
                else if (j == 0) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + 1]) + matrix[i][j];
                else if (j == n - 1) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + matrix[i][j];
                else dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j + 1], dp[i - 1][j - 1])) + matrix[i][j];
            }
        }
        int ret = dp[m - 1][0];
        for (int j = 1; j < n; j++)
            if (dp[m - 1][j] < ret) ret = dp[m - 1][j];
        return ret;
    }
};